package pl.squeaky.mvc.wizard;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;

public class NewControllerActionsPage extends WizardPage {

	private Button btnInit;
	private Button btnPreDispatch;
	private Button btnPostDispatch;
	private Table tblActions;
	private Button btnGenerateViews;

	protected NewControllerActionsPage() {
		super("NewControllerActionsPage");
		this.setTitle("New controller");
		this.setDescription("Customize actions and hooks");
	}

	@Override
	public void createControl(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		setControl(composite);
		
		Label lblActions = new Label(composite, SWT.NONE);
		lblActions.setText("Actions:");
		
		tblActions = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL);
		tblActions.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
		
		new TableItem(tblActions, SWT.NONE).setText("index");
		new TableItem(tblActions, SWT.NONE).setText("add");
		new TableItem(tblActions, SWT.NONE).setText("edit");
		new TableItem(tblActions, SWT.NONE).setText("delete");
		
		Composite cmpActions = new Composite(composite, SWT.NONE);
		cmpActions.setLayout(new GridLayout(2, false));
		cmpActions.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
		
		btnGenerateViews = new Button(cmpActions, SWT.CHECK);
		btnGenerateViews.setText("Generate associated &views");
		btnGenerateViews.setSelection(true);
		
		Button btnCustom = new Button(cmpActions, SWT.PUSH);
		btnCustom.setText("Add custom...");
		btnCustom.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END));
		btnCustom.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {		
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
				InputDialog dialog = new InputDialog(shell, "Add custom action", "Type action name", null, null);
				if(dialog.open() == Dialog.OK) {
					TableItem item = new TableItem(tblActions, SWT.NONE);
					item.setText(dialog.getValue());
					item.setChecked(true);
				}
			}
		});
		
		Label lblHooks = new Label(composite, SWT.NONE);
		lblHooks.setText("Hooks:");
		
		btnInit = new Button(composite, SWT.CHECK);
		btnInit.setText("init()");
		
		btnPreDispatch = new Button(composite, SWT.CHECK);
		btnPreDispatch.setText("preDispatch()");
		
		btnPostDispatch = new Button(composite, SWT.CHECK);
		btnPostDispatch.setText("postDispatch()");
	}
	
	public List<String> getActions() {
		ArrayList<String> actions = new ArrayList<String>();
		for(TableItem item : tblActions.getItems()) {
			if(item.getChecked()) {
				actions.add(item.getText());
			}
		}
		
		return actions;
	}
	
	public boolean getInit() {
		return btnInit.getSelection();
	}
	
	public boolean getPreDispatch() {
		return btnPreDispatch.getSelection();
	}

	public boolean getPostDispatch() {
		return btnPostDispatch.getSelection();
	}
	
	public boolean getGenerateViews() {
		return btnGenerateViews.getSelection();
	}
}
